<?
require_once('config.php');
require_once('../playlist/config.php');
$action = getValue('action','str','POST','');
$cat_id = getValue('cat_id');
$vid_id = getValue('id','int','GET',0);
if($vid_id > 0){
   
   /*-------- Lấy ra thông tin của video hiện tại từ bảng video ---------*/
   $array_info_vid = array();
   $db_query = new db_query("SELECT * FROM video 
                              WHERE vid_id= $vid_id");
   $array_info_vid = $db_query->resultArray();   
   /*-------------- Lấy ra tất cả ca sĩ của video hiện tại -------------*/
   
   $array_per = array();
   $sql_select = "SELECT 
                     person.per_id,per_name
                  FROM
                     person
                  WHERE
                     per_id IN(
                        SELECT 
                           vid_per.per_id 
                        FROM
                           vid_per 
                        WHERE 
                           vid_id = $vid_id)";
   $db_query  = new db_query($sql_select);
   $array_per = $db_query->resultArray();
   $array_json_per = array(); // Mảng sử dụng cho tokeninput plugin
   if(count($array_per) > 0){
      foreach($array_per as $value){
         $array_json_per[] = array('id' => $value['per_id'],
                                    'name' => $value['per_name']);   
      }
      
   }
}


// Nếu form được submit lên
if($action == 'submit'){
   $cat_name   = getValue('cat_name','str','POST');
   $vid_name   = getValue('vid_name','str','POST','');
   $vid_cat_id = getValue('vid_cat_id','int','POST');
   $vid_pla_id = getValue('vid_pla_id','int','POST');
   $vid_lyric  = getValue('vid_lyric','str','POST','');
   $vid_duration  = getValue('vid_duration','str','POST','');
   $vid_view      = getValue('vid_view','int','POST');
   $vid_image = getValue('vid_image','str','POST','');
   $per_id    = getValue('per_id','str','POST'); 
   
   //Update lại bảng vid_per
   // per_id là dạng chuỗi. Mỗi per_id được phân cách nhau bởi 1 dấu phảy ","
   $sql_del = 'DELETE FROM vid_per WHERE vid_id =' . $vid_id;
   mysql_query($sql_del);
   if($per_id != '') {
      
      $sql        = "INSERT INTO vid_per(vid_id,per_id) 
                         VALUES";
      $sql_value  = '';
      $array_per_id = explode(',',$per_id);
      foreach($array_per_id as $value){
         $sql_value .= "(" . $vid_id . "," . $value . "),";
      }
      $sql_value = substr($sql_value,0,-1);
      $sql .= $sql_value;
      $db_exe  = new db_execute($sql);
      unset($db_exe);
   }
   
   // Thực hiện update lại thông tin cho video
   $sql_update = "UPDATE 
                      video
                  SET vid_name = '" .$vid_name. "',
                      cat_id   = ". $vid_cat_id .",
                      pla_id   = ". $vid_pla_id .",
                      vid_image='".$vid_image."'     
                  WHERE 
                      vid_id = $vid_id";
   echo '<span style="color: #01B42C;font-size: 14px;font-weight: bold;">Cập nhật thành công</span>';
   $db_update = new db_execute($sql_update);
   
   /*-------- Lấy ra thông tin của video hiện tại từ bảng video ---------*/
   $array_info_vid = array();
   $db_query = new db_query("SELECT * FROM video 
                              WHERE vid_id= $vid_id");
   $array_info_vid = $db_query->resultArray();
   unset($db_query);
   
}
if($cat_id > 0){
   $array_info_cat = array();
   $db_query = new db_query(" SELECT 
                                 cat_name
                              FROM 
                                 category 
                              WHERE 
                                 cat_id=$cat_id");
   $array_info_cat = $db_query->resultArray();
   $cat_name = $array_info_cat[0]['cat_name'];
   
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Cập nhật <?=$moduol_name?></title>
   <?=$css_global?>
   <?=$js_global?>
  <script>
  $(function() {
    $(document).on('click','.reload_tab' ,function(){
      window.location.reload();
    });
  });
  </script>
</head>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Cập nhật <?=$moduol_name?></h1>      
      <div class="reload_tab" title="Reload"></div>                         
      <div class="clear"></div>
   </div>
   <div class="block-fluid">
      <form class="form-horizontal" name="add" action="" method="POST" enctype="multipart/form-data"  onsubmit="">
         <input type="hidden" name="action" value="submit" />
         <fieldset>
            <?if(count($array_info_vid) > 0):?>
            <?
               
               /*-------------- GET INFO VIDEO -------------- */
               $vid_name   = $array_info_vid[0]['vid_name'];
               $vid_cat_id = $array_info_vid[0]['cat_id'];
               $vid_pla_id = $array_info_vid[0]['pla_id'];
               $vid_lyric  = $array_info_vid[0]['vid_lyric'];
               $vid_view      = $array_info_vid[0]['vid_view'];
               $vid_image = $array_info_vid[0]['vid_image'];
               
            ?>
            <div class="control-group">
               <label class="control-label" for="input01">Tên video</label>
               <div class="controls">
						<input type="text" class="input-xlarge tooltip-right span8" value="<?=$vid_name?>" id="vid_name" name="vid_name" title="Tên video"/>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Thể loại</label>
               <div class="controls">
						<select id="vid_cat_id" name="vid_cat_id">
                     <option value="0">Không chọn</option>
                     <?if($count_cat > 0):?>
                        <?foreach($array_all_cat as $key=>$value):?>
                           <option value="<?=$key?>" <?if($key == $vid_cat_id) echo 'selected="true"'?>><?=$value['cat_name']?></option>
                        <?endforeach?>
                     <?endif?>
                  </select>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Playlist</label>
               <div class="controls">
						<select id="vid_pla_id" name="vid_pla_id">
                     <option value="0">Không chọn</option>
                     <?if($count_pla > 0):?>
                        <?foreach($array_all_pla as $key=>$value):?>
                           <option value="<?=$key?>" <?if($key == $vid_pla_id) echo 'selected="true"'?>><?=$value['pla_name']?></option>
                        <?endforeach?>
                     <?endif?>
                  </select>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Ca sĩ</label>
               <div class="controls">
						<input type="text" id="per_id" name="per_id" />
                 
                  <script type="text/javascript">
                  $(document).ready(function() {
                     $("#per_id").tokenInput("ajax_token_input_person.php", {
                        theme: "facebook",
                        preventDuplicates: true,
                        prePopulate: [
                                         <?foreach($array_json_per as $value){?>
                                          {id:<?=$value['id']?>,name:'<?=$value['name']?>'},
                                         <?}?>
                                        
                                     ],
                     });
                     
                  });
                  </script>
					</div>
            </div>
            <div class="control-group">
               <label class="control-label" for="input01">Lời bài hát</label>
               <div class="controls">
						<textarea  name="vid_lyric" id="vid_lyric"><?=$vid_lyric?></textarea>
                  <script type="text/javascript">CKEDITOR.replace('vid_lyric'); </script>
					</div>
            </div>
            <div class="form-actions">
					<button type="submit" class="btn btn-info">Cập nhật</button>
					<button type="reset"  class="btn btn-warning">Làm lại</button>
				</div>
            <?endif?>
         </fieldset>
      </form>
   </div> 
</div>